package love.yuanmengrobot.attendancesystem.dao;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/20 16:37
 */

import love.yuanmengrobot.attendancesystem.dao.SQL.SqlString;
import love.yuanmengrobot.attendancesystem.entity.AttendanceDate;
import love.yuanmengrobot.attendancesystem.entity.Attendancemain;
import love.yuanmengrobot.attendancesystem.entity.Attendancestudent;
import love.yuanmengrobot.attendancesystem.util.JdbcUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AttendanceDao {
    /**
     * 通过ID查询单条数据
     *
     * @param clazzid
     * @return 实例对象
     */
   public ArrayList<AttendanceDate> queryById(String clazzid) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            ArrayList<AttendanceDate> list = new ArrayList<AttendanceDate>();
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(SqlString.selectCQ(clazzid));
            //判断该学生签到状态
            while (rs.next()) {
                AttendanceDate attendanceDate = new AttendanceDate(rs.getString("a.atname"), rs.getString("count(*)"));
                list.add(attendanceDate);
            }
            if (list.size() > 0) return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }
        return null;
    }

    ;

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    List<Attendancemain> queryAllByLimit(int offset, int limit) {

        return null;
    }

    ;

    /**
     * 通过实体作为筛选条件查询
     *
     * @param attendance 实例对象
     * @return 对象列表
     */
    List<Attendancemain> queryAll(Attendancemain attendance) {

        return null;
    }

    ;

    /**
     * 新增数据,教师发起签到
     *
     * @param attendance 实例对象
     * @return 影响行数
     */
    public int insertMain(Attendancemain attendance) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            int s = stmt.executeUpdate(SqlString.insertAttendanceMain(attendance));
            if (s == 1) {
                return 1;
            } else {
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }


        return 0;
    }

    ;

    /**
     * 修改数据，教师结束签到
     *
     * @param
     * @return 影响行数
     */
    public int updateMain(String name, String id) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            int s = stmt.executeUpdate(SqlString.updateAttendanceMain(name, id));
            if (s == 1) {
                return 1;
            } else {
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }


        return 0;
    }

    ;

    public int selectAtidAndEndTime(String key,  String memberid, String address) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            Attendancestudent attendancestudent = new Attendancestudent();
            //查找id和结束时间


            rs = stmt.executeQuery(SqlString.selectAttendanceMainNULL(key));
            //判断该学生签到状态
            if (rs.next()) {
                attendancestudent.setAtsid(rs.getString("attendancemain.atid"));
                String s = rs.getString("attendancemain.atendtime");
                System.out.println("s:" + s);
                if (s == null) {
                    attendancestudent.setAtsstate("成功");
                } else {
                    attendancestudent.setAtsstate("迟到");
                }
            }else{
                return 0;
            }
            attendancestudent.setAtsmemberid(memberid);
            attendancestudent.setAddress(address);

            //学生进行签到
            int s = stmt.executeUpdate(SqlString.insertAttendanceStudent(attendancestudent));
            if (s == 1) {
                return 1;
            } else {
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }


        return 0;
    }

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param entities List<Attendance> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(List<Attendancemain> entities) {

        return 0;
    }

    ;

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<Attendance> 实例对象列表
     * @return 影响行数
     */
    int insertOrUpdateBatch(List<Attendancemain> entities) {

        return 0;
    }

    ;

    /**
     * 修改数据
     *
     * @param attendance 实例对象
     * @return 影响行数
     */
    int update(Attendancemain attendance) {

        return 0;
    }

    ;

    /**
     * 通过主键删除数据
     *
     * @param attendanceid 主键
     * @return 影响行数
     */
    int deleteById(Integer attendanceid) {

        return 0;
    }

    ;

}
